package com.iuie.ns.datum.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iuie.ns.datum.service.IBlogService;
import com.iuie.ns.datum.service.IBookInfoService;
import com.iuie.ns.datum.service.IFileService;
import com.iuie.ns.datum.vo.BookInfoVO;
import com.iuie.ns.frame.utils.StringUtils;
import com.iuie.ns.frame.web.dataset.vo.Page;
import com.iuie.ns.frame.web.response.vo.ResponseInfo;
import com.iuie.ns.system.service.code.ISysCodeInfoService;

@Controller
@RequestMapping(value="book")
public class BookController {
	
	private static final Logger logger = LoggerFactory.getLogger(BookController.class);

	@Autowired
	private IBookInfoService bookInfoService;
	
	@Autowired
	private ISysCodeInfoService sysCodeInfoService;
	
	@Autowired
	private IFileService fileService;
	
	@Autowired
	private IBlogService blogService;
	
	@RequestMapping(value="index")
	public String index(HttpServletRequest request){
		return "view-datum/book/book";
	}
	
	@RequestMapping(value="/find/all")
	@ResponseBody
	public ResponseInfo findAll(){
		return ResponseInfo.successInfoByObject(bookInfoService.findAll());
	}
	
	@RequestMapping(value="bookAddIndex")
	public String bookAddIndex(){
		return "view-datum/book/bookAdd";
	}
	
	
	@RequestMapping(value="findByPage")
	@ResponseBody
	public ResponseInfo index(BookInfoVO bookInfoVO, Page page){
		List<BookInfoVO> list = bookInfoService.queryList(page, bookInfoVO);
	    return ResponseInfo.successInfo(list, page);
	}
	
	@RequestMapping(value = "saveBook")
	@ResponseBody
	public ResponseInfo saveBook(BookInfoVO bookInfoVO) {
		BookInfoVO bookInfo = null;
		try {
			bookInfo = bookInfoService.saveOne(bookInfoVO);
			logger.info("新增博客成功：{}", bookInfo);
			return ResponseInfo.successInfo("博客保存成功！");
		} catch (Exception e) {
			 e.printStackTrace();
			 Long bookFileId = bookInfoVO.getUrlFileId();
			 Long coverFileId = bookInfoVO.getCoverFileId();
			 if(bookFileId != null){
				 fileService.deleteFileById(bookFileId, true);
			 }
			 if(coverFileId != null){
				 fileService.deleteFileById(coverFileId, true);
			 } 
		}
		return null;
		
	}

	@RequestMapping(value = "bookDelete/{bookId}")
	@ResponseBody
	public ResponseInfo bookDelete(@PathVariable("bookId")String bookId){
		 bookInfoService.deleteBook(bookId);
		 return ResponseInfo.successInfo("删除书籍成功！");
	}
	
	@RequestMapping(value = "update/index/{bookId}")
	public String updateIndex(@PathVariable("bookId")String bookId, HttpServletRequest request){
		BookInfoVO bookInfo = bookInfoService.findBookById(bookId);
		request.setAttribute("bookInfo", bookInfo);
		return "view-datum/book/update_book";
	}
	
	@RequestMapping(value="update/save")
	@ResponseBody
	public ResponseInfo updateSave(BookInfoVO bookInfoVO){
		if(StringUtils.isBlank(bookInfoVO.getBookId())){
			return ResponseInfo.errorInfo("更新书籍信息失败");
		}
		BookInfoVO bookInfo = bookInfoService.findBookById(bookInfoVO.getBookId());
		bookInfo.setComments(bookInfoVO.getComments());
		bookInfo.setProgress(bookInfoVO.getProgress());
		bookInfoService.updateBook(bookInfo);
		return ResponseInfo.successInfo("更新书籍信息成功");
	}
	
	@RequestMapping(value="blogBindBook")
	@ResponseBody
	public ResponseInfo blogBindBook(Long blogId, String bookId){
		blogService.updateBlogBookId(blogId,bookId);
		return ResponseInfo.successInfo("绑定博客成功");
	}
}
